package com.controller;

import com.interceptor.UserInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.model.User;
import com.validator.UserValidator;

@Before(UserInterceptor.class)
public class UserController extends Controller {
	/**
	 * 默认的index方法，未指定方法时，执行本方法
	 */
	public void index() {
		render("/user/index.html");
	}

	/**
	 * 用户注册
	 */
	public void regist() {
		String username = getPara("username");
		String password = getPara("password");
		User user = new User();
		user.set("UserName", username);
		user.set("PassWord", password);
		boolean flag = user.save();
		if (flag) {
			redirect("/index.html");
		} else {
			redirect("/regist.html");
		}
	}

	@Before(UserValidator.class)
	public void login() {
		String username = getPara("username");
		String password = getPara("password");
		User user = new User().login(username);
		String pwd = user.get("PassWord");
		if (password.equals(pwd)) {
			render("/");
		} else {
			render("/user/login.html");
		}
	}

	public void showAll() {
		setAttr("UserPage", new User().paginate(getParaToInt(0, 1), 10));
		render("user_info.jsp");
	}
}
